package cn.zhz.controller;

import cn.zhz.entity.User;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * 控制器
 */
@Controller
public class IndexController{
    private Logger logger=Logger.getLogger(IndexController.class);
    @RequestMapping("/index")
    public String index(){
        logger.info("hello,SpringMVC!");
        return "index";
    }

    @RequestMapping("/index1")
    public ModelAndView index(String username){
        logger.info("welcome!username:"+username);
        ModelAndView mView=new ModelAndView();
        mView.addObject("username",username);
        mView.setViewName("index");
        return mView;
    }
    @RequestMapping("/index2")
    public String index(String username,Model model){
        logger.info("hello,SpringMVC! username:"+username);
        model.addAttribute("username",username);
        model.addAttribute(username);
        User user=new User();
        user.setUserName(username);
        model.addAttribute("currentUser",user);
        model.addAttribute(user);
        return "index";
    }
    @RequestMapping("/index3")
    public String index(String username, Map<String,Object>model){
        logger.info("hello,SpringMVC! username:"+username);
        model.put("username",username);
        return "index";
    }

    @RequestMapping(value="/welcome",method = RequestMethod.GET,params = "username")
    public String welcome(@RequestParam(value = "username",required = false) String username){
        logger.info("welcome,"+username);
        return "index";
    }
}
